// 注册组件 withInstall
export const withInstall = (component, alias) => {
    component.install = (app) => {
      app.component(component.name || component.displayName, component)
      if (alias) {
        app.config.globalProperties[alias] = component
      }
    }
    return component
  }
  
  // 驼峰转下划线
  export const humpToUnderline = (str) => {
    return str.replace(/([A-Z])/g, '-$1').toLowerCase()
  }
  
  // 下划线转驼峰
  export const underlineToHump = (str) => {
    if (!str) return ''
    return str.replace(/\-(\w)/g, (_, letter) => letter.toUpperCase())
  }
  
  // 驼峰转横杠
  export const humpToDash = (str) => {
    return str.replace(/([A-Z])/g, '-$1').toLowerCase()
  }
  
  // 设置 CSS 变量
  export const setCssVar = (prop, val, dom = document.documentElement) => {
    dom.style.setProperty(prop, val)
  }
  
  // 获取 CSS 变量
  export const getCssVar = (prop, dom = document.documentElement) => {
    return getComputedStyle(dom).getPropertyValue(prop)
  }
  
  // 查找数组对象的某个下标
  export const findIndex = (ary, fn) => {
    if (ary.findIndex) {
      return ary.findIndex(fn)
    }
    let index = -1
    ary.some((item, i) => {
      if (fn(item, i, ary)) {
        index = i
        return true
      }
      return false
    })
    return index
  }
  
  // 去除字符串两端空格
  export const trim = (str) => {
    return str.replace(/(^\s*)|(\s*$)/g, '')
  }
  
  // 时间格式化
  export function formatTime(time, fmt) {
    if (!time) return ''
    const date = new Date(time)
    const o = {
      'M+': date.getMonth() + 1,
      'd+': date.getDate(),
      'H+': date.getHours(),
      'm+': date.getMinutes(),
      's+': date.getSeconds(),
      'q+': Math.floor((date.getMonth() + 3) / 3),
      S: date.getMilliseconds()
    }
    if (/(y+)/.test(fmt)) {
      fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
    }
    for (const k in o) {
      if (new RegExp('(' + k + ')').test(fmt)) {
        fmt = fmt.replace(
          RegExp.$1,
          RegExp.$1.length === 1 ? o[k] : ('00' + o[k]).substr(('' + o[k]).length)
        )
      }
    }
    return fmt
  }
  
  // 生成随机字符串（伪 UUID）
  export function toAnyString() {
    return 'xxxxx-xxxxx-4xxxx-yxxxx-xxxxx'.replace(/[xy]/g, (c) => {
      const r = (Math.random() * 16) | 0
      const v = c === 'x' ? r : (r & 0x3) | 0x8
      return v.toString(16)
    })
  }
  
  // 首字母大写
  export function firstUpperCase(str) {
    return str.toLowerCase().replace(/( |^)[a-z]/g, (L) => L.toUpperCase())
  }
  
  // 对象转 FormData
  export function objToFormData(obj) {
    const formData = new FormData()
    Object.keys(obj).forEach((key) => {
      formData.append(key, obj[key])
    })
    return formData
  }
  